#!/bin/bash
###
# @Author: Logan.Li
# @Date: 2025-09-05 15:00:17
# @LastEditTime: 2025-09-09 00:55:00
# @Description: Jenkins环境依赖安装脚本 - 调用独立的JDK、Maven、NVM安装脚本
###
# curl -s https://gitee.com/attacker/All-In-One-Ops/raw/master/1.scripts/services/jenkins-env.sh | bash

# 基础设置
set -e  # 遇到错误立即退出

# 简洁输出函数
info() { echo -e "\e[34m[INFO]\e[0m $1"; }
warn() { echo -e "\e[33m[WARN]\e[0m $1"; }
error() { echo -e "\e[31m[ERROR]\e[0m $1" >&2; }
success() { echo -e "\e[32m[SUCCESS]\e[0m $1"; }

# 检查是否为root权限
check_root() {
    if [ $(id -u) -ne 0 ]; then
        error "### 此脚本必须以root权限运行 !!!"
        exit 1
    fi
}

# 安装依赖脚本
install_dependency() {
    local name="$1"
    local check_cmd="$2"
    local install_url="$3"
    
    info "检查 $name..."
    if command -v "$check_cmd" &> /dev/null; then
        success "$name 已安装"
        return 0
    fi
    
    warn "$name 未安装，开始安装..."
    info "下载安装脚本: $install_url"
    
    # 添加超时和错误处理
    if curl -m 30 -s "$install_url" | bash; then
        success "$name 安装完成"
    else
        error "$name 安装失败，请检查网络连接"
        return 1
    fi
}

# 安装Java JDK17
install_java() {
    install_dependency "JDK17" "java" "https://gitee.com/attacker/All-In-One-Ops/raw/master/1.scripts/env/jdk17.sh"
}

# 安装Maven
install_maven() {
    install_dependency "Maven" "mvn" "https://gitee.com/attacker/All-In-One-Ops/raw/master/1.scripts/env/maven.sh"
}

# 安装Node.js (通过NVM)
install_nodejs() {
    install_dependency "NVM和Node.js" "node" "https://gitee.com/attacker/All-In-One-Ops/raw/master/1.scripts/env/nvm.sh"
}

# 验证安装结果
verify_installation() {
    info "验证Jenkins环境依赖安装结果..."
    
    echo "========================================="
    echo "Jenkins环境依赖安装验证:"
    echo "========================================="
    
    # 验证Java
    if command -v java &> /dev/null; then
        echo "✓ Java安装成功"
        echo "  版本信息: $(java -version 2>&1 | head -n1)"
    else
        echo "✗ Java安装失败"
    fi
    
    echo ""
    
    # 验证Maven
    if command -v mvn &> /dev/null; then
        echo "✓ Maven安装成功"
        echo "  版本信息: $(mvn -version 2>/dev/null | head -n1 || echo '版本信息获取失败')"
    else
        echo "✗ Maven安装失败"
    fi
    
    echo ""
    
    # 验证Node.js
    if command -v node &> /dev/null; then
        echo "✓ Node.js安装成功"
        echo "  Node.js版本: $(node -v 2>/dev/null || echo '版本信息获取失败')"
        echo "  NPM版本: $(npm -v 2>/dev/null || echo '版本信息获取失败')"
    else
        echo "✗ Node.js安装失败"
    fi
    
    echo ""
    echo "========================================="
    echo "使用说明:"
    echo "========================================="
    echo "1. 重新登录或执行以下命令使环境变量生效:"
    echo "   source /etc/profile && source ~/.bashrc"
    echo ""
    echo "2. 各组件常用命令:"
    echo "   java -version        # 查看Java版本"
    echo "   mvn -version         # 查看Maven版本"
    echo "   node -v              # 查看Node.js版本"
    echo "   npm -v               # 查看NPM版本"
    echo "   nvm list             # 查看已安装的Node.js版本"
    echo "========================================="
}

# 主函数
main() {
    info "开始安装Jenkins环境依赖..."
    info "将安装: Java JDK17、Apache Maven、Node.js (via NVM)"
    info ""
    
    check_root
    
    # 检查必要命令
    if ! command -v curl &> /dev/null; then
        info "安装curl..."
        if command -v yum &> /dev/null; then
            yum install -y curl
        elif command -v apt-get &> /dev/null; then
            apt-get update && apt-get install -y curl
        fi
    fi
    
    # 安装各个组件（调用独立脚本）
    install_java
    install_maven
    install_nodejs
    
    # 验证安装结果
    verify_installation
    
    success "Jenkins环境依赖安装完成！"
}

# 执行主函数
main "$@"